var fs = require("fs");
var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    database : 'qrsoft_v1_tangshi'
});
connection.connect();
//遍历目录
const rootpath = "E:\\爬虫目录\\temp\\古诗\\古诗内容";
var index = 0;
getfolder();
//遍历作者、朝代、关键字
var dic0={};
var dic1={};
var dic2={};

function getfolder() {
    //写入顶级目录
    insertclass(0, "顶级目录", function (cid0) {
        insertclass(cid0, "唐诗", function (cid1) {
            fs.readdir(rootpath, function (err0, menu0) {
                if (menu0) {
                    // console.log('年代目录：'+menu0);
                    menu0.forEach(function (ele0) {
                        //写入年代目录
                        // adddynasty(ele0);
                        fs.readdir(rootpath + '\\' + ele0, function (err1, menu1) {
                            // console.log('作者目录：'+menu1);
                            if (menu1) {
                                menu1.forEach(function (ele1) {
                                    // addauthor(ele0,ele1);
                                    fs.readdir(rootpath + '\\' + ele0 + '\\' + ele1, function (err2, menu2) {
                                        menu2.forEach(function (ele2) {
                                            // console.log('诗：'+ele2);
                                            var data = fs.readFileSync(rootpath + '\\' + ele0 + '\\' + ele1 + '\\' + ele2);
                                            var json = JSON.parse(data);
                                            //写入文章
                                            insertarticle(cid1, ele2.substr(ele2, ele2.length - 4), json.content,json.yi, json.tags.join(','),json.author,json.dynasty);
                                            // addtags(json.tags);
                                        });
                                    });
                                })
                            }
                        });
                    });
                }
            });
        });
    });
}
function adddynasty(dynasty){
    if(dic0[dynasty]==undefined){
        dic0[dynasty]='';
        console.log(dynasty);
        inserttag(3,'年代',dynasty);
    }
}

function addauthor(dynasty,author){
    if(dic1[dynasty]==undefined){
        dic1[dynasty]={};
        dic1[dynasty][author]='';
        console.log(dynasty+author)
        inserttag(4,dynasty,author);
    }else{
        if(dic1[dynasty][author]==undefined){
            dic1[dynasty][author]='';
            inserttag(4,dynasty,author);
            console.log(dynasty+author);
        }
    }
}

function addtags(tags){
    for (let index = 0; index < tags.length; index++) {
        const element = tags[index];
        if(dic2[element]==undefined){
            dic2[element]='';
            inserttag(5,'标签',element);
            console.log(element);
        }
    }
}
function inserttag(tag_type,tag_class,tag_name){
    var sql = "insert into qr_sys_tags(tag_type,tag_class,tag_name)values(?,?,?);";
    var addSqlParams = [tag_type,tag_class,tag_name];
    connection.query(sql,addSqlParams,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        } 
        console.log(result);
    });
}

function insertclass(pid,classname,fn){
    fn(0);
    return false;
    var sql = "insert into qr_article_class(pid,class_name,path,group_id)values(?,?,?,1);";
    var addSqlParams = [pid,classname,'c'+(++index)];
    connection.query(sql,addSqlParams,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        } 
        console.log(result);
        if(fn)fn(result.insertId);
    });
}

function insertarticle(classid,title,content,content1,tags,author,dynasty){
    return false;
    var sql = "insert into qr_article(class_id,title,short_title,content,content1,model_type,status)values(?,?,?,?,?,6,2);";
    var addSqlParams = [classid,title,title,content,content1];
    connection.query(sql,addSqlParams,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        } 
        console.log(result);
        insertarticlemodel6(result.insertId,tags,author,dynasty);
    });
}

function insertarticlemodel6(articleid,tags,author,dynasty){
    var sql = "insert into qr_article_model_6(article_id,tags,author,dynasty)values(?,?,?,?);";
    var addSqlParams = [articleid,tags,author,dynasty];
    connection.query(sql,addSqlParams,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }  
        console.log(result);
    });

}